package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.eduorder.service.PayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author zr
 * @since 2021-11-25
 */
@RestController
@RequestMapping("/edu/order/log")
//@CrossOrigin
@Api(tags = "微信支付管理")
public class PayLogController {
    @Resource
    private PayLogService payLogService;

    @ApiOperation("生成微信支付二维码")
    @GetMapping("createNativeCode/{orderNo}")
    public Result createNativeCode(
            @ApiParam(name = "orderNo", value = "订单号", required = true)
            @PathVariable String orderNo) {
        // 根据订单号生成二维码
        Map map = payLogService.createNativeCode(orderNo);
        System.out.println("返回的二维码集合"+map);
        return Result.ok(map);
    }

    @ApiOperation("根据订单号查询支付状态")
    @GetMapping("queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo) {
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("返回的订单状态集合"+map);
        if (map == null) {
            return Result.fail().message("支付过程出错了");
        }
        // 通过map获取订单状态
        if ("SUCCESS".equals(map.get("trade_state"))) {
            //  添加支付记录到支付表 更新订单状态
            payLogService.updateOrderStatus(map);
            return Result.ok().message("支付成功");
        }
        return Result.ok().code(25000).message("支付中");
    }
}

